import { Controller } from "../../mvc/core/Controller";
import { ISexChangeController } from "./interfaces/ISexChangeController";
import { IView } from "../../mvc/interfaces/IView";
import { SexChangeModule } from "../../../modules/SexChangeModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { GameProxy } from "../mvc/GameProxy";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { DataEvent } from "../events/DataEvent";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { ShopConfig } from "../resource/ShopConfig";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { ItemConfig } from "../resource/ItemConfig";
import { ISexChangeModule } from "../../../modules/interfaces/ISexChangeModule";
import { ShopItemInfo } from "../resource/info/ShopItemInfo";
import { _shopItemInfo } from "../../../this/_shopItemInfo";
type int = number;
//class SexChangeController
    
    export  class SexChangeController extends Controller implements ISexChangeController
    {
       

        protected /* override */ initView(): IView
        {
            if (this._sexChangeModule == null) 
            {
                this._sexChangeModule = new SexChangeModule();
                this._sexChangeModule.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onSexChangeModuleAddedToStage);
            }
            return this._sexChangeModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.SexChangeOpen, this.onSexChangeOpenHandler);
            Dispatcher.addEventListener(EventName.SexToChange, this.onSexToChangeHandler);
            Dispatcher.addEventListener(EventName.SexChangeSuccess, this.onSexChangeSuccessHandler);
            Dispatcher.addEventListener(EventName.SexChangeGetCdSuccess, this.onSexChangeGetCdSuccessHandler);
            Dispatcher.addEventListener(EventName.ShopMallBuyItemSuccess, this.onShopMallBuyItemSuccessHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onServerBagDataChangehandler);
            return;
        }

        /* internal  */onSexChangeModuleAddedToStage(arg1: Object /* flash.events.Event */): void
        {
            if (this._cdDays == -1) 
            {
                GameProxy.roleProxy.canChangeSexAfterDays();
            }
            if (this._sexChangeModule) 
            {
                this._sexChangeModule.getPropNum(ECategory._ECategoryProp, EProp._EPropChangeSex);
            }
            return;
        }

        /* internal  */onSexChangeOpenHandler(arg1: DataEvent): void
        {
            view.show();
            NpcEffectRule.registDialogEffectWindow(view);
            return;
        }

        /* internal  */onSexToChangeHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            if (loc1 && this._cdDays <= 0) 
            {
                if (this._shopItemInfo == null) 
                {
                    this._shopItemInfo = ShopConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropChangeSex);
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, {"shopItemInfo": _shopItemInfo, "amount": 1}));
            }
            else 
            {
                GameProxy.roleProxy.changeSex(this._sexChangeModule.changeSex, this._sexChangeModule.broadcast);
            }
            this._isToChangeSex = true;
            return;
        }

        /* internal  */onSexChangeSuccessHandler(arg1: DataEvent): void
        {
            if (this._sexChangeModule) 
            {
                this._sexChangeModule.changeSexSuccess();
                GameProxy.roleProxy.canChangeSexAfterDays();
                this._sexChangeModule.hide();
                MsgManager.showRollTipsMsg(Language.getString(36017));
            }
            this._isToChangeSex = false;
            return;
        }

        /* internal  */onSexChangeGetCdSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (this._sexChangeModule) 
            {
                this._sexChangeModule.updateCd(loc1);
            }
            return;
        }

        /* internal  */onShopMallBuyItemSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["itemCode"] as int;
            var loc2=arg1.data["amount"] as int;
            var loc3;
            if ((loc3 = ItemConfig.instance.getInfoByCode(loc1)).item.category == ECategory._ECategoryProp && loc3.item.type == EProp._EPropChangeSex && this._sexChangeModule) 
            {
                this._sexChangeModule.updatePropNum(loc2);
                if (this._isToChangeSex) 
                {
                    GameProxy.roleProxy.changeSex(this._sexChangeModule.changeSex, this._sexChangeModule.broadcast);
                }
            }
            return;
        }

        /* internal  */onServerBagDataChangehandler(arg1: Object): void
        {
            if (this._sexChangeModule && !this._sexChangeModule.isHide) 
            {
                this._sexChangeModule.getPropNum(ECategory._ECategoryProp, EProp._EPropChangeSex);
            }
            return;
        }

        private /* var */_sexChangeModule: ISexChangeModule;

        private /* var */_cdDays: int=-1;

        private /* var */_shopItemInfo: ShopItemInfo;

        private /* var */_isToChangeSex: Boolean=false;
    }
